package me.zhengjie.repository;

import me.zhengjie.dto.sqlServer.DeviceStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @website https://eladmin.vip
 * @author zhangYY
 * @date 2024-01-13
 **/
public interface DeviceStatusRepository  extends JpaRepository<DeviceStatus, Integer>, JpaSpecificationExecutor<DeviceStatus> {
    @Query("select d.status from DeviceStatus d where d.code =?1")
    Integer getStatusByCode(String code);

    @Query("select ds from DeviceStatus ds where ds.code=?1")
    DeviceStatus findOldDeviceStatus(String code);

    @Modifying
    @Query("update DeviceStatus ds set ds.aisle=?1, ds.layer=?2, ds.status=?3, ds.lastUpdated=?4 where ds.code=?5")
    void updateDeviceStatus(Integer aisle, Integer layer, Integer status, LocalDateTime timestamp, String code);

    @Modifying
    @Query(value = "insert into device_status(code,equipment_type,aisle,layer,status) values(?1,?2,?3,?4,?5)",nativeQuery = true)
    void saveNewStatus(String code, Integer equipmentType, Integer aisle, Integer layer, Integer status);

    @Query("select ds from DeviceStatus ds order by ds.lastUpdated desc")
    List<DeviceStatus> findNewDeviceStatus();
}
